package com.biggiant.project.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import java.util.List;

/**
 * Created by congl on 2017/8/8.
 */

public class ActivityUtils {

    public static void addFragmentToActivity(FragmentManager manager, int resId, Fragment fragment) {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(resId, fragment, fragment.getClass().getSimpleName());
        transaction.commit();
    }

    public static void showFragment(FragmentManager fragmentManager, int resId, Fragment fragment) {
        Fragment showFragment = fragmentManager.findFragmentByTag(fragment.getClass().getSimpleName());
        if (showFragment != null) {
            fragmentManager.beginTransaction().show(showFragment).commit();
        } else {
            addFragmentToActivity(fragmentManager, resId, fragment);
        }
    }

    public static void hideFragment(FragmentManager fragmentManager, int resId, Fragment fragment) {
        Fragment hideFragment = fragmentManager.findFragmentByTag(fragment.getClass().getSimpleName());
        if (hideFragment != null) {
            fragmentManager.beginTransaction().hide(hideFragment).commit();
        }
    }

    public static void removeFragment(FragmentManager fragmentManager, int resId, Fragment fragment) {
        Fragment removeFragment = fragmentManager.findFragmentByTag(fragment.getClass().getSimpleName());
        if (removeFragment != null) {
            fragmentManager.beginTransaction().remove(removeFragment);
        }
    }

    public static void switchFragmentByAnim(FragmentManager fragmentManager, int resId, Fragment switchFragment) {

        switchFragmentByAnim(fragmentManager,
                resId,
                switchFragment,
                android.R.anim.slide_in_left,
                android.R.anim.slide_out_right,
                android.R.anim.slide_in_left,
                android.R.anim.slide_out_right);

    }

    public static void switchFragmentByAnim(FragmentManager fragmentManager, int resId, Fragment switchFragment, int inAnimId1, int inAnimId2, int outAnimId1, int outAnimId2) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction
                .setCustomAnimations(inAnimId1, outAnimId1, inAnimId2, outAnimId2)
                .replace(resId, switchFragment)
                .addToBackStack("detail")
                .commit();
    }
}
